package org.glutenberg.util;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class LinkedHashtable {
	
	private Vector keys;
	private Vector elements;
	private Hashtable positions;
	
	public LinkedHashtable(){
		this.keys = new Vector();
		this.elements = new Vector();
		this.positions = new Hashtable();
	}
	
	public Object put(Object key, Object value){
		Object ret = null;
		Integer pos = null;
		if(!this.positions.containsKey(key)){
			pos = new Integer(this.keys.size());
			this.keys.addElement(key);
			this.elements.addElement(value);
		} else {
			pos = (Integer)this.positions.get(key);
			ret = this.elements.elementAt(pos.intValue());
			this.elements.setElementAt(value, pos.intValue());
		}
		this.positions.put(key, pos);
		return ret;
	}
	
	public Object get(Object key){
		Object ret = null;
		if(this.positions.containsKey(key)){
			ret = this.elements.elementAt(((Integer)this.positions.get(key)).intValue());
		}
		return ret;
	}
	
	public Vector keysVector(){
		return this.keys;
	}
	
	public Vector elementsVector(){
		return this.elements;
	}
	
	public Integer getPosition(Object key){
		return (Integer)this.positions.get(key);
	}
	
	public Enumeration keys(){
		return this.keys.elements();
	}
	
	public Enumeration elements(){
		return this.elements.elements();
	}
}
